package com.shanzmoo.core.domain;

import com.shanzmoo.base.constant.DeliverySendType;
import com.shanzmoo.base.util.BillNoUtils;
import com.shanzmoo.core.cache.CurrentUserCache;
import com.shanzmoo.db.logistics.entity.DeliveryBillEntity;
import com.shanzmoo.db.logistics.entity.DeliveryLogisticsDetailEntity;
import com.shanzmoo.db.logistics.entity.DeliverySkuDetailEntity;
import com.shanzmoo.db.order.entity.OutletOrderDetailEntity;
import com.shanzmoo.db.order.entity.dto.LogisticsDeliverDto;

import java.util.ArrayList;
import java.util.List;

/**
 * 发货单生成辅助类
 *
 * @author: Gzhao 2020/12/16
 */
public class DeliveryBillCreator {

    private DeliveryBillCreator() {
    }

    public static DeliveryBillEntity createBill(String orderNo, Integer orderType) {
        return DeliveryBillEntity.builder()
                .billNo(BillNoUtils.generateDeliveryNo())
                .orderNo(orderNo)
                .orderType(orderType)
                .createUser(CurrentUserCache.getUserId())
                .build();
    }

    /**
     * 自提，生成发货单物流明细
     *
     * @param deliveryId 发货单id
     * @return
     */
    public static List<DeliveryLogisticsDetailEntity> createNoLogisticsDetailList(Integer deliveryId) {
        DeliveryLogisticsDetailEntity detail = DeliveryLogisticsDetailEntity.builder()
                .deliveryId(deliveryId)
                .sendType(DeliverySendType.self)
                .build();
        List<DeliveryLogisticsDetailEntity> reList = new ArrayList<>();
        reList.add(detail);

        return reList;
    }

    /**
     * 发物流，生成发货单物流明细
     * @param deliveryId 发货单id
     * @param logisticsDetailList
     * @return
     */
    public static List<DeliveryLogisticsDetailEntity> createLogisticsDetailList(Integer deliveryId, List<LogisticsDeliverDto.LogisticsDetail> logisticsDetailList) {
        List<DeliveryLogisticsDetailEntity> reList = new ArrayList<>();
        logisticsDetailList.forEach(e->{
            DeliveryLogisticsDetailEntity detail = DeliveryLogisticsDetailEntity.builder()
                    .deliveryId(deliveryId)
                    .sendType(DeliverySendType.logistics)
                    .logisticsNo(e.getLogisticsNo())
                    .logisticsCompany(e.getLogisticsCompany())
                    .build();
        });
        return reList;
    }

    public static List<DeliverySkuDetailEntity> createSkuDetailList(List<OutletOrderDetailEntity> detailList, Integer deliveryId) {
        List<DeliverySkuDetailEntity> reList = new ArrayList<>();

        for (OutletOrderDetailEntity orderDetail : detailList) {
            DeliverySkuDetailEntity skuDetail = DeliverySkuDetailEntity.builder()
                    .deliveryId(deliveryId)
                    .sku(orderDetail.getSku())
                    .skuName(orderDetail.getSkuName())
                    .unit(orderDetail.getUnit())
                    .attributeDesc(orderDetail.getAttributeDesc())
                    .skuNum(orderDetail.getSkuNum())
                    .build();
            reList.add(skuDetail);
        }
        return reList;
    }

    public static List<DeliverySkuDetailEntity> createSkuDetailList2(List<LogisticsDeliverDto.SkuDetail> detailList, Integer deliveryId) {
        List<DeliverySkuDetailEntity> reList = new ArrayList<>();

        detailList.forEach(e->{
            DeliverySkuDetailEntity skuDetail = DeliverySkuDetailEntity.builder()
                    .deliveryId(deliveryId)
                    .sku(e.getSku())
                    .skuName(e.getSkuName())
                    .unit(e.getUnit())
                    .attributeDesc(e.getAttributeDesc())
                    .skuNum(e.getSkuNum())
                    .build();
            reList.add(skuDetail);
        });
        return reList;
    }
}
